﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria.Graphics;
using Terraria.Utilities;

namespace Terraria.GameContent.Liquid
{
	// Token: 0x0200042D RID: 1069
	public class LiquidRenderer
	{
		// Token: 0x1400003C RID: 60
		// (add) Token: 0x0600250E RID: 9486 RVA: 0x00482594 File Offset: 0x00480794
		// (remove) Token: 0x0600250F RID: 9487 RVA: 0x004825CC File Offset: 0x004807CC
		public event Action<Color[], Rectangle> WaveFilters;

		// Token: 0x06002510 RID: 9488 RVA: 0x00482604 File Offset: 0x00480804
		public LiquidRenderer()
		{
			for (int i = 0; i < this._liquidTextures.Length; i++)
			{
				this._liquidTextures[i] = TextureManager.Load("Images/Misc/water_" + i);
			}
		}

		// Token: 0x06002511 RID: 9489 RVA: 0x00482694 File Offset: 0x00480894
		private unsafe void InternalPrepareDraw(Rectangle drawArea)
		{
			Rectangle rectangle = new Rectangle(drawArea.X - 2, drawArea.Y - 2, drawArea.Width + 4, drawArea.Height + 4);
			this._drawArea = drawArea;
			if (this._cache.Length < rectangle.Width * rectangle.Height + 1)
			{
				this._cache = new LiquidRenderer.LiquidCache[rectangle.Width * rectangle.Height + 1];
			}
			if (this._drawCache.Length < drawArea.Width * drawArea.Height + 1)
			{
				this._drawCache = new LiquidRenderer.LiquidDrawCache[drawArea.Width * drawArea.Height + 1];
			}
			if (this._waveMask.Length < drawArea.Width * drawArea.Height)
			{
				this._waveMask = new Color[drawArea.Width * drawArea.Height];
			}
			fixed (LiquidRenderer.LiquidCache* ptr = &this._cache[1])
			{
				LiquidRenderer.LiquidCache* ptr2 = ptr;
				int num = rectangle.Height * 2 + 2;
				LiquidRenderer.LiquidCache* ptr3 = ptr2;
				for (int i = rectangle.X; i < rectangle.X + rectangle.Width; i++)
				{
					for (int j = rectangle.Y; j < rectangle.Y + rectangle.Height; j++)
					{
						Tile tile = Main.tile[i, j];
						if (tile == null)
						{
							tile = new Tile();
						}
						ptr3->LiquidLevel = (float)tile.liquid / 255f;
						ptr3->IsHalfBrick = (tile.halfBrick() && ptr3[-1].HasLiquid);
						ptr3->IsSolid = (WorldGen.SolidOrSlopedTile(tile) && !ptr3->IsHalfBrick);
						ptr3->HasLiquid = (tile.liquid > 0);
						ptr3->VisibleLiquidLevel = 0f;
						ptr3->HasWall = (tile.wall > 0);
						ptr3->Type = tile.liquidType();
						if (ptr3->IsHalfBrick && !ptr3->HasLiquid)
						{
							ptr3->Type = ptr3[-1].Type;
						}
						ptr3++;
					}
				}
				ptr3 = ptr2;
				ptr3 += num;
				for (int k = 2; k < rectangle.Width - 2; k++)
				{
					for (int l = 2; l < rectangle.Height - 2; l++)
					{
						float num2 = 0f;
						if (ptr3->IsHalfBrick && ptr3[-1].HasLiquid)
						{
							num2 = 1f;
						}
						else if (!ptr3->HasLiquid)
						{
							LiquidRenderer.LiquidCache liquidCache = ptr3[-rectangle.Height];
							LiquidRenderer.LiquidCache liquidCache2 = ptr3[rectangle.Height];
							LiquidRenderer.LiquidCache liquidCache3 = ptr3[-1];
							LiquidRenderer.LiquidCache liquidCache4 = ptr3[1];
							if (liquidCache.HasLiquid && liquidCache2.HasLiquid && liquidCache.Type == liquidCache2.Type)
							{
								num2 = liquidCache.LiquidLevel + liquidCache2.LiquidLevel;
								ptr3->Type = liquidCache.Type;
							}
							if (liquidCache3.HasLiquid && liquidCache4.HasLiquid && liquidCache3.Type == liquidCache4.Type)
							{
								num2 = Math.Max(num2, liquidCache3.LiquidLevel + liquidCache4.LiquidLevel);
								ptr3->Type = liquidCache3.Type;
							}
							num2 *= 0.5f;
						}
						else
						{
							num2 = ptr3->LiquidLevel;
						}
						ptr3->VisibleLiquidLevel = num2;
						ptr3->HasVisibleLiquid = (num2 != 0f);
						ptr3++;
					}
					ptr3 += 4;
				}
				ptr3 = ptr2;
				for (int m = 0; m < rectangle.Width; m++)
				{
					for (int n = 0; n < rectangle.Height - 10; n++)
					{
						if (ptr3->HasVisibleLiquid && !ptr3->IsSolid)
						{
							ptr3->Opacity = 1f;
							ptr3->VisibleType = ptr3->Type;
							float num3 = 1f / (float)(LiquidRenderer.WATERFALL_LENGTH[(int)ptr3->Type] + 1);
							float num4 = 1f;
							for (int num5 = 1; num5 <= LiquidRenderer.WATERFALL_LENGTH[(int)ptr3->Type]; num5++)
							{
								num4 -= num3;
								if (ptr3[num5].IsSolid)
								{
									break;
								}
								ptr3[num5].VisibleLiquidLevel = Math.Max(ptr3[num5].VisibleLiquidLevel, ptr3->VisibleLiquidLevel * num4);
								ptr3[num5].Opacity = num4;
								ptr3[num5].VisibleType = ptr3->Type;
							}
						}
						if (ptr3->IsSolid)
						{
							ptr3->VisibleLiquidLevel = 1f;
							ptr3->HasVisibleLiquid = false;
						}
						else
						{
							ptr3->HasVisibleLiquid = (ptr3->VisibleLiquidLevel != 0f);
						}
						ptr3++;
					}
					ptr3 += 10;
				}
				ptr3 = ptr2;
				ptr3 += num;
				for (int num6 = 2; num6 < rectangle.Width - 2; num6++)
				{
					for (int num7 = 2; num7 < rectangle.Height - 2; num7++)
					{
						if (!ptr3->HasVisibleLiquid || ptr3->IsSolid)
						{
							ptr3->HasLeftEdge = false;
							ptr3->HasTopEdge = false;
							ptr3->HasRightEdge = false;
							ptr3->HasBottomEdge = false;
						}
						else
						{
							LiquidRenderer.LiquidCache liquidCache5 = ptr3[-1];
							LiquidRenderer.LiquidCache liquidCache6 = ptr3[1];
							LiquidRenderer.LiquidCache liquidCache7 = ptr3[-rectangle.Height];
							LiquidRenderer.LiquidCache liquidCache8 = ptr3[rectangle.Height];
							float num8 = 0f;
							float num9 = 1f;
							float num10 = 0f;
							float num11 = 1f;
							float visibleLiquidLevel = ptr3->VisibleLiquidLevel;
							if (!liquidCache5.HasVisibleLiquid)
							{
								num10 += liquidCache6.VisibleLiquidLevel * (1f - visibleLiquidLevel);
							}
							if (!liquidCache6.HasVisibleLiquid && !liquidCache6.IsSolid && !liquidCache6.IsHalfBrick)
							{
								num11 -= liquidCache5.VisibleLiquidLevel * (1f - visibleLiquidLevel);
							}
							if (!liquidCache7.HasVisibleLiquid && !liquidCache7.IsSolid && !liquidCache7.IsHalfBrick)
							{
								num8 += liquidCache8.VisibleLiquidLevel * (1f - visibleLiquidLevel);
							}
							if (!liquidCache8.HasVisibleLiquid && !liquidCache8.IsSolid && !liquidCache8.IsHalfBrick)
							{
								num9 -= liquidCache7.VisibleLiquidLevel * (1f - visibleLiquidLevel);
							}
							ptr3->LeftWall = num8;
							ptr3->RightWall = num9;
							ptr3->BottomWall = num11;
							ptr3->TopWall = num10;
							Point zero = Point.Zero;
							ptr3->HasTopEdge = ((!liquidCache5.HasVisibleLiquid && !liquidCache5.IsSolid) || num10 != 0f);
							ptr3->HasBottomEdge = ((!liquidCache6.HasVisibleLiquid && !liquidCache6.IsSolid) || num11 != 1f);
							ptr3->HasLeftEdge = ((!liquidCache7.HasVisibleLiquid && !liquidCache7.IsSolid) || num8 != 0f);
							ptr3->HasRightEdge = ((!liquidCache8.HasVisibleLiquid && !liquidCache8.IsSolid) || num9 != 1f);
							if (!ptr3->HasLeftEdge)
							{
								if (ptr3->HasRightEdge)
								{
									zero.X += 32;
								}
								else
								{
									zero.X += 16;
								}
							}
							if (ptr3->HasLeftEdge && ptr3->HasRightEdge)
							{
								zero.X = 16;
								zero.Y += 32;
								if (ptr3->HasTopEdge)
								{
									zero.Y = 16;
								}
							}
							else if (!ptr3->HasTopEdge)
							{
								if (!ptr3->HasLeftEdge && !ptr3->HasRightEdge)
								{
									zero.Y += 48;
								}
								else
								{
									zero.Y += 16;
								}
							}
							if (zero.Y == 16 && (ptr3->HasLeftEdge ^ ptr3->HasRightEdge) && (num7 + rectangle.Y) % 2 == 0)
							{
								zero.Y += 16;
							}
							ptr3->FrameOffset = zero;
						}
						ptr3++;
					}
					ptr3 += 4;
				}
				ptr3 = ptr2;
				ptr3 += num;
				for (int num12 = 2; num12 < rectangle.Width - 2; num12++)
				{
					for (int num13 = 2; num13 < rectangle.Height - 2; num13++)
					{
						if (ptr3->HasVisibleLiquid)
						{
							LiquidRenderer.LiquidCache liquidCache9 = ptr3[-1];
							LiquidRenderer.LiquidCache liquidCache10 = ptr3[1];
							LiquidRenderer.LiquidCache liquidCache11 = ptr3[-rectangle.Height];
							LiquidRenderer.LiquidCache liquidCache12 = ptr3[rectangle.Height];
							ptr3->VisibleLeftWall = ptr3->LeftWall;
							ptr3->VisibleRightWall = ptr3->RightWall;
							ptr3->VisibleTopWall = ptr3->TopWall;
							ptr3->VisibleBottomWall = ptr3->BottomWall;
							if (liquidCache9.HasVisibleLiquid && liquidCache10.HasVisibleLiquid)
							{
								if (ptr3->HasLeftEdge)
								{
									ptr3->VisibleLeftWall = (ptr3->LeftWall * 2f + liquidCache9.LeftWall + liquidCache10.LeftWall) * 0.25f;
								}
								if (ptr3->HasRightEdge)
								{
									ptr3->VisibleRightWall = (ptr3->RightWall * 2f + liquidCache9.RightWall + liquidCache10.RightWall) * 0.25f;
								}
							}
							if (liquidCache11.HasVisibleLiquid && liquidCache12.HasVisibleLiquid)
							{
								if (ptr3->HasTopEdge)
								{
									ptr3->VisibleTopWall = (ptr3->TopWall * 2f + liquidCache11.TopWall + liquidCache12.TopWall) * 0.25f;
								}
								if (ptr3->HasBottomEdge)
								{
									ptr3->VisibleBottomWall = (ptr3->BottomWall * 2f + liquidCache11.BottomWall + liquidCache12.BottomWall) * 0.25f;
								}
							}
						}
						ptr3++;
					}
					ptr3 += 4;
				}
				ptr3 = ptr2;
				ptr3 += num;
				for (int num14 = 2; num14 < rectangle.Width - 2; num14++)
				{
					for (int num15 = 2; num15 < rectangle.Height - 2; num15++)
					{
						if (ptr3->HasLiquid)
						{
							LiquidRenderer.LiquidCache liquidCache13 = ptr3[-1];
							LiquidRenderer.LiquidCache liquidCache14 = ptr3[1];
							LiquidRenderer.LiquidCache liquidCache15 = ptr3[-rectangle.Height];
							LiquidRenderer.LiquidCache liquidCache16 = ptr3[rectangle.Height];
							if (ptr3->HasTopEdge && !ptr3->HasBottomEdge && (ptr3->HasLeftEdge ^ ptr3->HasRightEdge))
							{
								if (ptr3->HasRightEdge)
								{
									ptr3->VisibleRightWall = liquidCache14.VisibleRightWall;
									ptr3->VisibleTopWall = liquidCache15.VisibleTopWall;
								}
								else
								{
									ptr3->VisibleLeftWall = liquidCache14.VisibleLeftWall;
									ptr3->VisibleTopWall = liquidCache16.VisibleTopWall;
								}
							}
							else if (liquidCache14.FrameOffset.X == 16 && liquidCache14.FrameOffset.Y == 32)
							{
								if (ptr3->VisibleLeftWall > 0.5f)
								{
									ptr3->VisibleLeftWall = 0f;
									ptr3->FrameOffset = new Point(0, 0);
								}
								else if (ptr3->VisibleRightWall < 0.5f)
								{
									ptr3->VisibleRightWall = 1f;
									ptr3->FrameOffset = new Point(32, 0);
								}
							}
						}
						ptr3++;
					}
					ptr3 += 4;
				}
				ptr3 = ptr2;
				ptr3 += num;
				for (int num16 = 2; num16 < rectangle.Width - 2; num16++)
				{
					for (int num17 = 2; num17 < rectangle.Height - 2; num17++)
					{
						if (ptr3->HasLiquid)
						{
							LiquidRenderer.LiquidCache liquidCache17 = ptr3[-1];
							LiquidRenderer.LiquidCache liquidCache18 = ptr3[1];
							LiquidRenderer.LiquidCache liquidCache19 = ptr3[-rectangle.Height];
							LiquidRenderer.LiquidCache liquidCache20 = ptr3[rectangle.Height];
							if (!ptr3->HasBottomEdge && !ptr3->HasLeftEdge && !ptr3->HasTopEdge && !ptr3->HasRightEdge)
							{
								if (liquidCache19.HasTopEdge && liquidCache17.HasLeftEdge)
								{
									ptr3->FrameOffset.X = Math.Max(4, (int)(16f - liquidCache17.VisibleLeftWall * 16f)) - 4;
									ptr3->FrameOffset.Y = 48 + Math.Max(4, (int)(16f - liquidCache19.VisibleTopWall * 16f)) - 4;
									ptr3->VisibleLeftWall = 0f;
									ptr3->VisibleTopWall = 0f;
									ptr3->VisibleRightWall = 1f;
									ptr3->VisibleBottomWall = 1f;
								}
								else if (liquidCache20.HasTopEdge && liquidCache17.HasRightEdge)
								{
									ptr3->FrameOffset.X = 32 - Math.Min(16, (int)(liquidCache17.VisibleRightWall * 16f) - 4);
									ptr3->FrameOffset.Y = 48 + Math.Max(4, (int)(16f - liquidCache20.VisibleTopWall * 16f)) - 4;
									ptr3->VisibleLeftWall = 0f;
									ptr3->VisibleTopWall = 0f;
									ptr3->VisibleRightWall = 1f;
									ptr3->VisibleBottomWall = 1f;
								}
							}
						}
						ptr3++;
					}
					ptr3 += 4;
				}
				ptr3 = ptr2;
				ptr3 += num;
				fixed (LiquidRenderer.LiquidDrawCache* ptr4 = &this._drawCache[0])
				{
					LiquidRenderer.LiquidDrawCache* ptr5 = ptr4;
					fixed (Color* ptr6 = &this._waveMask[0])
					{
						Color* ptr7 = ptr6;
						LiquidRenderer.LiquidDrawCache* ptr8 = ptr5;
						Color* ptr9 = ptr7;
						for (int num18 = 2; num18 < rectangle.Width - 2; num18++)
						{
							for (int num19 = 2; num19 < rectangle.Height - 2; num19++)
							{
								if (ptr3->HasVisibleLiquid)
								{
									float num20 = Math.Min(0.75f, ptr3->VisibleLeftWall);
									float num21 = Math.Max(0.25f, ptr3->VisibleRightWall);
									float num22 = Math.Min(0.75f, ptr3->VisibleTopWall);
									float num23 = Math.Max(0.25f, ptr3->VisibleBottomWall);
									if (ptr3->IsHalfBrick && num23 > 0.5f)
									{
										num23 = 0.5f;
									}
									ptr8->IsVisible = (ptr3->HasWall || !ptr3->IsHalfBrick || !ptr3->HasLiquid);
									ptr8->SourceRectangle = new Rectangle((int)(16f - num21 * 16f) + ptr3->FrameOffset.X, (int)(16f - num23 * 16f) + ptr3->FrameOffset.Y, (int)Math.Ceiling((double)((num21 - num20) * 16f)), (int)Math.Ceiling((double)((num23 - num22) * 16f)));
									ptr8->IsSurfaceLiquid = (ptr3->FrameOffset.X == 16 && ptr3->FrameOffset.Y == 0 && (double)(num19 + rectangle.Y) > Main.worldSurface - 40.0);
									ptr8->Opacity = ptr3->Opacity;
									ptr8->LiquidOffset = new Vector2((float)Math.Floor((double)(num20 * 16f)), (float)Math.Floor((double)(num22 * 16f)));
									ptr8->Type = ptr3->VisibleType;
									ptr8->HasWall = ptr3->HasWall;
									byte b = LiquidRenderer.WAVE_MASK_STRENGTH[(int)ptr3->VisibleType];
									byte b2 = (byte)(b >> 1);
									ptr9->R = b2;
									ptr9->G = b2;
									ptr9->B = LiquidRenderer.VISCOSITY_MASK[(int)ptr3->VisibleType];
									ptr9->A = b;
									LiquidRenderer.LiquidCache* ptr10 = ptr3 - 1;
									if (num19 != 2 && !ptr10->HasVisibleLiquid && !ptr10->IsSolid && !ptr10->IsHalfBrick)
									{
										*(ptr9 - 1) = *ptr9;
									}
								}
								else
								{
									ptr8->IsVisible = false;
									int num24 = (!ptr3->IsSolid && !ptr3->IsHalfBrick) ? 4 : 3;
									byte b3 = LiquidRenderer.WAVE_MASK_STRENGTH[num24];
									byte b4 = (byte)(b3 >> 1);
									ptr9->R = b4;
									ptr9->G = b4;
									ptr9->B = LiquidRenderer.VISCOSITY_MASK[num24];
									ptr9->A = b3;
								}
								ptr3++;
								ptr8++;
								ptr9++;
							}
							ptr3 += 4;
						}
					}
				}
				ptr3 = ptr2;
				for (int num25 = rectangle.X; num25 < rectangle.X + rectangle.Width; num25++)
				{
					for (int num26 = rectangle.Y; num26 < rectangle.Y + rectangle.Height; num26++)
					{
						if (ptr3->VisibleType == 1 && ptr3->HasVisibleLiquid && Dust.lavaBubbles < 200)
						{
							if (this._random.Next(700) == 0)
							{
								Dust.NewDust(new Vector2((float)(num25 * 16), (float)(num26 * 16)), 16, 16, 35, 0f, 0f, 0, Color.White, 1f);
							}
							if (this._random.Next(350) == 0)
							{
								int num27 = Dust.NewDust(new Vector2((float)(num25 * 16), (float)(num26 * 16)), 16, 8, 35, 0f, 0f, 50, Color.White, 1.5f);
								Main.dust[num27].velocity *= 0.8f;
								Dust dust = Main.dust[num27];
								dust.velocity.X = dust.velocity.X * 2f;
								Dust dust2 = Main.dust[num27];
								dust2.velocity.Y = dust2.velocity.Y - (float)this._random.Next(1, 7) * 0.1f;
								if (this._random.Next(10) == 0)
								{
									Dust dust3 = Main.dust[num27];
									dust3.velocity.Y = dust3.velocity.Y * (float)this._random.Next(2, 5);
								}
								Main.dust[num27].noGravity = true;
							}
						}
						ptr3++;
					}
				}
			}
			if (this.WaveFilters != null)
			{
				this.WaveFilters(this._waveMask, this.GetCachedDrawArea());
			}
		}

		// Token: 0x06002512 RID: 9490 RVA: 0x00483958 File Offset: 0x00481B58
		private unsafe void InternalDraw(SpriteBatch spriteBatch, Vector2 drawOffset, int waterStyle, float globalAlpha, bool isBackgroundDraw)
		{
			Rectangle drawArea = this._drawArea;
			Main.tileBatch.Begin();
			fixed (LiquidRenderer.LiquidDrawCache* ptr = &this._drawCache[0])
			{
				LiquidRenderer.LiquidDrawCache* ptr2 = ptr;
				LiquidRenderer.LiquidDrawCache* ptr3 = ptr2;
				for (int i = drawArea.X; i < drawArea.X + drawArea.Width; i++)
				{
					for (int j = drawArea.Y; j < drawArea.Y + drawArea.Height; j++)
					{
						if (ptr3->IsVisible)
						{
							Rectangle sourceRectangle = ptr3->SourceRectangle;
							if (ptr3->IsSurfaceLiquid)
							{
								sourceRectangle.Y = 1280;
							}
							else
							{
								sourceRectangle.Y += this._animationFrame * 80;
							}
							Vector2 liquidOffset = ptr3->LiquidOffset;
							float num = ptr3->Opacity * (isBackgroundDraw ? 1f : LiquidRenderer.DEFAULT_OPACITY[(int)ptr3->Type]);
							int num2 = (int)ptr3->Type;
							if (num2 == 0)
							{
								num2 = waterStyle;
								num *= (isBackgroundDraw ? 1f : globalAlpha);
							}
							else if (num2 == 2)
							{
								num2 = 11;
							}
							num = Math.Min(1f, num);
							VertexColors colors;
							Lighting.GetColor4Slice_New(i, j, out colors, 1f);
							colors.BottomLeftColor *= num;
							colors.BottomRightColor *= num;
							colors.TopLeftColor *= num;
							colors.TopRightColor *= num;
							Main.tileBatch.Draw(this._liquidTextures[num2], new Vector2((float)(i << 4), (float)(j << 4)) + drawOffset + liquidOffset, new Rectangle?(sourceRectangle), colors, Vector2.Zero, 1f, SpriteEffects.None);
						}
						ptr3++;
					}
				}
			}
			Main.tileBatch.End();
		}

		// Token: 0x06002513 RID: 9491 RVA: 0x00483B44 File Offset: 0x00481D44
		public bool HasFullWater(int x, int y)
		{
			x -= this._drawArea.X;
			y -= this._drawArea.Y;
			int num = x * this._drawArea.Height + y;
			return num < 0 || num >= this._drawCache.Length || (this._drawCache[num].IsVisible && !this._drawCache[num].IsSurfaceLiquid);
		}

		// Token: 0x06002514 RID: 9492 RVA: 0x00483BBC File Offset: 0x00481DBC
		public float GetVisibleLiquid(int x, int y)
		{
			x -= this._drawArea.X;
			y -= this._drawArea.Y;
			if (x < 0 || x >= this._drawArea.Width || y < 0 || y >= this._drawArea.Height)
			{
				return 0f;
			}
			int num = (x + 2) * (this._drawArea.Height + 4) + y + 2;
			if (!this._cache[num].HasVisibleLiquid)
			{
				return 0f;
			}
			return this._cache[num].VisibleLiquidLevel;
		}

		// Token: 0x06002515 RID: 9493 RVA: 0x00483C54 File Offset: 0x00481E54
		public void Update(GameTime gameTime)
		{
			if (Main.gamePaused || !Main.hasFocus)
			{
				return;
			}
			float num = Main.windSpeed * 80f;
			num = MathHelper.Clamp(num, -20f, 20f);
			if (num < 0f)
			{
				num = Math.Min(-10f, num);
			}
			else
			{
				num = Math.Max(10f, num);
			}
			this._frameState += num * (float)gameTime.ElapsedGameTime.TotalSeconds;
			if (this._frameState < 0f)
			{
				this._frameState += 16f;
			}
			this._frameState %= 16f;
			this._animationFrame = (int)this._frameState;
		}

		// Token: 0x06002516 RID: 9494 RVA: 0x0001A1DD File Offset: 0x000183DD
		public void PrepareDraw(Rectangle drawArea)
		{
			this.InternalPrepareDraw(drawArea);
		}

		// Token: 0x06002517 RID: 9495 RVA: 0x00483D0C File Offset: 0x00481F0C
		public void SetWaveMaskData(ref Texture2D texture)
		{
			if (texture == null || texture.Width < this._drawArea.Height || texture.Height < this._drawArea.Width)
			{
				Console.WriteLine("WaveMaskData texture recreated. {0}x{1}", this._drawArea.Height, this._drawArea.Width);
				if (texture != null)
				{
					try
					{
						texture.Dispose();
					}
					catch
					{
					}
				}
				texture = new Texture2D(Main.instance.GraphicsDevice, this._drawArea.Height, this._drawArea.Width, false, SurfaceFormat.Color);
			}
			texture.SetData<Color>(0, new Rectangle?(new Rectangle(0, 0, this._drawArea.Height, this._drawArea.Width)), this._waveMask, 0, this._drawArea.Width * this._drawArea.Height);
		}

		// Token: 0x06002518 RID: 9496 RVA: 0x0001A1E6 File Offset: 0x000183E6
		public Rectangle GetCachedDrawArea()
		{
			return this._drawArea;
		}

		// Token: 0x06002519 RID: 9497 RVA: 0x0001A1EE File Offset: 0x000183EE
		public void Draw(SpriteBatch spriteBatch, Vector2 drawOffset, int waterStyle, float alpha, bool isBackgroundDraw)
		{
			this.InternalDraw(spriteBatch, drawOffset, waterStyle, alpha, isBackgroundDraw);
		}

		// Token: 0x0600251A RID: 9498 RVA: 0x00483E00 File Offset: 0x00482000
		static LiquidRenderer()
		{
			byte[] array = new byte[5];
			array[3] = byte.MaxValue;
			LiquidRenderer.WAVE_MASK_STRENGTH = array;
			byte[] array2 = new byte[5];
			array2[1] = 200;
			array2[2] = 240;
			LiquidRenderer.VISCOSITY_MASK = array2;
			LiquidRenderer.Instance = new LiquidRenderer();
		}

		// Token: 0x040040E0 RID: 16608
		private const int ANIMATION_FRAME_COUNT = 16;

		// Token: 0x040040E1 RID: 16609
		private const int CACHE_PADDING = 2;

		// Token: 0x040040E2 RID: 16610
		private const int CACHE_PADDING_2 = 4;

		// Token: 0x040040E3 RID: 16611
		public const float MIN_LIQUID_SIZE = 0.25f;

		// Token: 0x040040E4 RID: 16612
		private static readonly int[] WATERFALL_LENGTH = new int[]
		{
			10,
			3,
			2
		};

		// Token: 0x040040E5 RID: 16613
		private static readonly float[] DEFAULT_OPACITY = new float[]
		{
			0.6f,
			0.95f,
			0.95f
		};

		// Token: 0x040040E6 RID: 16614
		private static readonly byte[] WAVE_MASK_STRENGTH;

		// Token: 0x040040E7 RID: 16615
		private static readonly byte[] VISCOSITY_MASK;

		// Token: 0x040040E8 RID: 16616
		public static LiquidRenderer Instance;

		// Token: 0x040040E9 RID: 16617
		public Texture2D[] _liquidTextures = new Texture2D[12];

		// Token: 0x040040EA RID: 16618
		private LiquidRenderer.LiquidCache[] _cache = new LiquidRenderer.LiquidCache[1];

		// Token: 0x040040EB RID: 16619
		private LiquidRenderer.LiquidDrawCache[] _drawCache = new LiquidRenderer.LiquidDrawCache[1];

		// Token: 0x040040EC RID: 16620
		private int _animationFrame;

		// Token: 0x040040ED RID: 16621
		private Rectangle _drawArea = new Rectangle(0, 0, 1, 1);

		// Token: 0x040040EE RID: 16622
		private UnifiedRandom _random = new UnifiedRandom();

		// Token: 0x040040EF RID: 16623
		private Color[] _waveMask = new Color[1];

		// Token: 0x040040F0 RID: 16624
		private float _frameState;

		// Token: 0x0200042E RID: 1070
		private struct LiquidCache
		{
			// Token: 0x040040F2 RID: 16626
			public float LiquidLevel;

			// Token: 0x040040F3 RID: 16627
			public float VisibleLiquidLevel;

			// Token: 0x040040F4 RID: 16628
			public float Opacity;

			// Token: 0x040040F5 RID: 16629
			public bool IsSolid;

			// Token: 0x040040F6 RID: 16630
			public bool IsHalfBrick;

			// Token: 0x040040F7 RID: 16631
			public bool HasLiquid;

			// Token: 0x040040F8 RID: 16632
			public bool HasVisibleLiquid;

			// Token: 0x040040F9 RID: 16633
			public bool HasWall;

			// Token: 0x040040FA RID: 16634
			public Point FrameOffset;

			// Token: 0x040040FB RID: 16635
			public bool HasLeftEdge;

			// Token: 0x040040FC RID: 16636
			public bool HasRightEdge;

			// Token: 0x040040FD RID: 16637
			public bool HasTopEdge;

			// Token: 0x040040FE RID: 16638
			public bool HasBottomEdge;

			// Token: 0x040040FF RID: 16639
			public float LeftWall;

			// Token: 0x04004100 RID: 16640
			public float RightWall;

			// Token: 0x04004101 RID: 16641
			public float BottomWall;

			// Token: 0x04004102 RID: 16642
			public float TopWall;

			// Token: 0x04004103 RID: 16643
			public float VisibleLeftWall;

			// Token: 0x04004104 RID: 16644
			public float VisibleRightWall;

			// Token: 0x04004105 RID: 16645
			public float VisibleBottomWall;

			// Token: 0x04004106 RID: 16646
			public float VisibleTopWall;

			// Token: 0x04004107 RID: 16647
			public byte Type;

			// Token: 0x04004108 RID: 16648
			public byte VisibleType;
		}

		// Token: 0x0200042F RID: 1071
		private struct LiquidDrawCache
		{
			// Token: 0x04004109 RID: 16649
			public Rectangle SourceRectangle;

			// Token: 0x0400410A RID: 16650
			public Vector2 LiquidOffset;

			// Token: 0x0400410B RID: 16651
			public bool IsVisible;

			// Token: 0x0400410C RID: 16652
			public float Opacity;

			// Token: 0x0400410D RID: 16653
			public byte Type;

			// Token: 0x0400410E RID: 16654
			public bool IsSurfaceLiquid;

			// Token: 0x0400410F RID: 16655
			public bool HasWall;
		}
	}
}
